Uec3.InventoryWindow = Ext.extend(Ext.app.Module, {
	id : 'inventory',
	type : 'common/inventory',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {

	},
	paramsCollection : new Ext.util.MixedCollection(true),

	getParamsByKey : function(key) {
		return this.paramsCollection.get(key);
	},
	getComponents : function(scope) {

		var inventory_grid;
		var inventory_ds;
		var inventory_form;
		var inventory_store_ds;
		var inventory_storeLevel1;
		var inventory_storeLevel2;
		var inventory_comboLevel1;
		var inventory_comboLevel2;
		var inventory_storePublicLevel;
		var inventory_comboPublicLevel;
		var inventory_middleValue;


		var getAndRestoreInventory = function() {
			
			inventory_store_ds = new Ext.data.JsonStore({
				proxy : new Ext.data.HttpProxy({
					url : Uec3.App.Constants.homeUrl+ '/store/base!query.do'
				
				}),
				reader : new Ext.data.JsonReader({
					root:'root',totalProperty: "totalProperty"},
					[
					{name : 'id',type:'int'},
					{name : 'name',type:'string'},
					{name : 'pyCode',type:'string'}
				])
			});

			inventory_storeLevel1 = new Ext.data.Store({
						proxy : new Ext.data.HttpProxy({
									url : Uec3.App.Constants.homeUrl
											+ '/part/category_getFirstCategorys.do'
								}),
						reader : new Ext.data.JsonReader({
									root : 'root'
								}, [{
											name : 'id'
										}, {
											name : 'level1Name'
										}])
					});

			inventory_storePublicLevel = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
					url : Uec3.App.Constants.homeUrl
							+ '/dictionary/findByVarGroup.do?group=partproperty_publicanLevel'
				}),
				reader : new Ext.data.JsonReader({
							root : '',
							fields : [{
										name : 'varIndex'
									}, {
										name : 'varName'
									}]
						})
			});

			inventory_storeLevel2 = new Ext.data.Store({
						proxy : new Ext.data.HttpProxy({
									url : Uec3.App.Constants.homeUrl
											+ '/part/category_getSubCategoryList.do'
								}),
						reader : new Ext.data.JsonReader({
									root : 'root',
									fields : [{
												name : 'id'
											}, {
												name : 'level2Name'
											}]
								})
					});

			inventory_ds = new Ext.data.Store({

				proxy : new Ext.data.HttpProxy({
							url : Uec3.App.Constants.homeUrl
									+ '/inventory/browse_inventory!browseInventory.do'
						}),
				baseParams : {
					start : 0,
					limit : 20
				},
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'root'
						}, [

						{
									name : 'id'
								},

								{
									name : 'companyName'
								}, {
									name : 'storeName'
								},

								{

									name : 'partName'
								}, {
									name : 'partCode'
								}, {

									name : 'partSourceName'
								}, {
									name : 'brand'
								}, {
									name : 'origin'
								}, {
									name : 'cost'
								}, {
									name : 'retailPrice'
								}, {
									name : 'insurancePrice'
								}, {
									name : 'minPrice'
								}, {
									name : 'initQuantity'
								}, {
									name : 'onHand'
								},

								{
									name : 'publicanLevel'
								}, {
									name : 'documentTypeName'
								}, {
									name : 'documentCode'
								}, {
									name : 'partnerName'
								},

								{
									name : 'gmtEntry',
									type : 'string'
								}, {
									id:"gmtModify",
									name : 'gmtModify',
									type : 'string'
								}]),
				remoteSort : true,

				// 默认排序字段
				sortInfo : {
					field : "id",
					direction : "asc"
				}

			});

		};

		var componentInitsInventory = function() {

			// 初始化combox对象
			
			var storeLookupbox = new Ext.form.LookupBox2({
				fieldLabel : '仓库',
		        store:inventory_store_ds ,
		        displayField:'name',
		        lookupField:'pyCode',
		        valueField:'id',
		        hiddenName:"storeId",
		        typeAhead: false,
		        mode: 'remote',
		        triggerAction: 'all',
		        emptyText:'请选择仓库...',
		        selectOnFocus:true, 			
		        queryDelay:10,
		        typeAheadDelay:10,
		        pageSize:5,
		        autoDestory:true,
		        queryParam:'keyword',
		        app:scope.app,
		     	triggerTags:[{moduleId: 'store_list',triggerClass:'x-form-search-trigger'},{moduleId: 'store_add'}]
		   });
			
			// 初始化combox对象
			inventory_comboPublicLevel = new Ext.form.ComboBox({
						hiddenName : 'publicanLevel',
						store : inventory_storePublicLevel,
						emptyText : '--请选择--',
						mode : 'local',
						triggerAction : 'all',
						width : 160,
						valueField : 'varIndex',
						displayField : 'varName',
						loadingText : '加载中...',
						forceSelection : true,
						editable : false,
						selectOnFocus : true
					});

			inventory_storePublicLevel.load();

			inventory_comboLevel1 = new Ext.form.ComboBox({
						name : 'level1Name',
						store : inventory_storeLevel1,
						selectOnFocus : true,
						forceSelection : true,
						emptyText : '请选择一级分类',
						width : 160,
						mode : 'local',
						triggerAction : 'all',
						valueField : 'id',
						displayField : 'level1Name',
						loadingText : '加载中...',
						editable : false

					});

			inventory_storeLevel1.load();

			inventory_comboLevel2 = new Ext.form.ComboBox({
						name : 'level2Name',
						selectOnFocus : true,
						forceSelection : false,
						width : 160,
						store : inventory_storeLevel2,
						emptyText : '请选择二级分类',
						mode : 'local',
						triggerAction : 'all',
						valueField : 'id',
						displayField : 'level2Name',
						loadingText : '加载中...',
						editable : false
					})

			// 初始化inventory表格对象

			var inventory_colModel = new Ext.grid.ColumnModel([
							new Ext.grid.RowNumberer(),

			             {
								header : "公司名称",
								width : 80,
								dataIndex : 'companyName'
							}, {
								header : "仓库名称",
								width : 80,
								dataIndex : 'storeName'
							}, {
								header : "零件名称",
								width : 80,
								dataIndex : 'partName'
							},

							{
								id : 'partCode',
								header : "零件编码",
								width : 100,
								sortable : true,
								dataIndex : 'partCode'

							}, {
								header : "零件来源",
								width : 100,
								dataIndex : 'partSourceName'
							},

							{
								header : "品牌",
								width : 80,
								dataIndex : 'brand'
							}, {
								header : "产地",
								width : 80,
								dataIndex : 'origin'
							}, {
								header : "成本价",
								width : 80,
								dataIndex : 'cost'
							}, {
								header : "公开零售价",
								width : 80,
								dataIndex : 'retailPrice'
							}, {
								header : "保险价",
								width : 60,
								dataIndex : 'insurancePrice'
							}, {
								header : "最低售价",
								width : 80,
								dataIndex : 'minPrice'
							}, {
								header : "初始数量",
								width : 80,
								dataIndex : 'initQuantity'

							}, {
								header : "剩余数量",
								width : 80,
								dataIndex : 'onHand'

							}, {
								header : "公开对象",
								width : 80,
								dataIndex : 'publicanLevel',
								renderer : Ext.util.Format
										.comboRenderer(inventory_comboPublicLevel)
							}, {
								header : "单据名称",
								width : 80,
								dataIndex : 'documentTypeName'
							}, {
								header : "单据号",
								width : 80,
								dataIndex : 'documentCode'
							}, {
								header : "供应商名称",
								width : 80,
								dataIndex : 'partnerName'
							}, {
								header : "入仓时间",
								width : 120,
								dataIndex : 'gmtEntry'
							}, {
								header : "修改时间",
								width : 120,
								dataIndex : 'gmtModify',
								id : 'gmtModify',
								align : 'center'

							}

					]);

			// create the Grid
			inventory_grid = new Ext.grid.GridPanel(

			{
						region : 'center',
						autoScroll : true,
						iconCls : 'icon-grid',
						enableColumnMove : false,
						enableColumnMoveResize : false,
						stripeRows : true,
						autoExpandColumn : 'gmtModify',
						width : '100%',
						height : 500,
						// autoExpandColumn:'gmtModify',
						loadMask : {
							msg : "...加载数据中"
						},
						store : inventory_ds,
						cm : inventory_colModel,
						sm : new Ext.grid.RowSelectionModel({
									singleSelect : true
								}),

						viewConfig : {
							// forceFit :true,
							columnsText : '显示的列',
							scrollOffset : 20,
							sortAscText : '升序',
							sortDescText : '降序'
						},
						bbar : new Ext.PagingToolbar({
									pageSize : 20,
									store : inventory_ds,
									display : true,
									displayInfo : true,
									displayMsg : '第 {0} - {1} 条  共 {2} 条',
									emptyMsg : "没有记录",
									items : [/**
												 * '-', { pressed: true,
												 * enableToggle:true, text:
												 * 'Show Preview', cls:
												 * 'x-btn-text-icon details',
												 * toggleHandler: function(btn,
												 * pressed){ var view =
												 * inventory_grid.getView();
												 * view.showPreview = pressed;
												 * view.refresh(); } }
												 */
									]
								})

					});
			// 把数据加载到store中
		//	inventory_ds.load();

			// 初始化表单对象
			inventory_form = new Ext.form.FormPanel({
				labelAlign : 'right',
				border : false,
				labelWidth : 60,
				layout : 'form',
				frame : true,
				width : '100%',
				autoHeight : true,
				url : '',
				items : [{
							xtype : 'panel',
							layout : 'hbox',
							items : [{
										xtype : 'label',
										text : '仓库 :',
										width : 36,
										style : "margin-top:2px;"

									}, storeLookupbox, {
										xtype : 'label',
										text : '一级分类 :',
										width : 68,
										style : "margin-top:2px;margin-left:12px;"

									}, inventory_comboLevel1, {
										xtype : 'label',
										text : '二级分类 :',
										width : 68,
										style : "margin-top:2px;margin-left:12px;"

									}, inventory_comboLevel2, {
										xtype : 'label',
										text : '商品编码 :',
										width : 68,
										style : "margin-top:2px;margin-left:12px;"

									}, {
										xtype : 'textfield',
										name : 'code'
									}]
						}, {
							xtype : 'panel',
							layout : 'hbox',
							style : "margin-top:12px;",
							items : [

							{
										xtype : 'label',
										text : '库存下限和上限数量 :',
										width : 120,
										style : "margin-top:2px;"

									}, {
										xtype : 'numberfield',
										type : 'number',
										name : 'lowerQuantity'
									},{
										xtype : 'label',
										text : '——',
										style :"font-size:12px"

									},{
										xtype : 'numberfield',
										type : 'number',
										name : 'upperQuantity'
									},

									{
										border : false,
										style : "margin-left:12px;",
										xtype : 'button',
										width : 80,
										text : '查询',
										// iconCls :'query',
										anchor : '80%',
										handler : function() {
										
										var upperQuantity=inventory_form.getForm().findField("upperQuantity");
										var lowerQuantity=inventory_form.getForm().findField("lowerQuantity");

										if(upperQuantity.getValue()<lowerQuantity.getValue()){
											
											Ext.Msg.alert("提示","库存下限数量不能大于上限数量");
											
											return;
										}
										
										
											if (!inventory_form.getForm()
													.isValid()) {

												Ext.Msg.alert('提示', "表单校验未通过");

												return;

											}

											var formValues = inventory_form
													.getForm().getValues();
											var jsonAll = Ext
													.encode(formValues);

											inventory_ds.baseParams = {
												start : 0,
												limit : 20,
												jsonAll : jsonAll
											}
											inventory_ds.reload();

										}
									}]
						},

						{
							html : "<div style='height:8px'><div>"
						}, inventory_grid]
			});

		};

		var controlPartPropertyEvent = function() {
          
			inventory_storeLevel1.on('load',function(store){
				var p=new store.recordType({"level1Name":"请选择一级分类", id:-1});
				store.insert(0, p);
			})
			
			inventory_storeLevel2.on('load',function(store){
				var p=new store.recordType({"level2Name":"请选择二级分类", id:-1});
				store.insert(0, p);
			})
			
			
			inventory_comboLevel1.on('select', function(comboBox) {

						var value = comboBox.getValue();
						inventory_comboLevel2.clearValue();
						inventory_storeLevel2.setBaseParam('level1Id', value);
						inventory_storeLevel2.load();
					});

			inventory_comboLevel2.on('select', function(comboBox) {

						var value = comboBox.getValue();

						inventory_middleValue = value;

					});

			inventory_comboLevel2.on('blur', function(comboBox) {

						comboBox.setValue(inventory_middleValue);

					});
		};


		getAndRestoreInventory();

		componentInitsInventory();

		controlPartPropertyEvent();
		this.paramsCollection.add("inventory_ds", inventory_ds);
		return inventory_form;

	},
	initEvent:function(){
		var module=this;
		var win=module.getParamsByKey("win");
		win.on('show',function(){
			this.getParamsByKey("inventory_ds").load({
				 callback: function(records,options,success){
		         
		         if(success){
		        	 
		        	// Ext.Msg.alert('提示',"加载数据成功");
		         }else{
		        	 
		        	 
		        	 Ext.Msg.alert('提示',"网络连接异常...请稍候再试");
		         }			  
			
		 }			
	
	});
		},module,{single:true});
	},
	/**
	 * Create this modules window here.
	 */
	createWindow : function() {
		var desktop = this.app.getDesktop();
		var module=this;
		var win = desktop.getWindow('inventory');
		if (!win) {
			win = desktop.createWindow({
				id : 'inventory',
				title : '库存浏览',
				width : document.body.clientWidth * 0.9,
				height : document.body.clientHeight * 0.9,
				iconCls : 'acc-icon',
				shim : false,
				animCollapse : false,
				constrainHeader : true,
				maximizable : true,
				taskbuttonTooltip : "库存浏览",
				layout : "fit",
				listeners : {
					'resize' : function(win, width, height) {
						if (width < 900 || height < 500) {
							win.setWidth(900);
							win.setHeight(500);
							this.items.get(0).items.last().setHeight(500 - 100);
						} else {
							this.items.get(0).items.last().setHeight(height
									- 100);
						}
					}
				},
				tools : [{
							id : 'refresh',
							handler : Ext.emptyFn,
							scope : this
						}],
				items : [this.getComponents(this)]
			});
			module.paramsCollection.add("win",win);
		    module.initEvent();
		}
		win.show();

	}
});